
#include "Stdafx.h"
#include "SimpleWindow.h"


CWindow::CWindow()
{
	m_hWnd = 0;
}
CWindow::CWindow( HWND hWnd )
{
	Attach( hWnd );
}

CWindow& CWindow::operator=( HWND hWnd )
{
	Attach( hWnd );
	return *this;
}
void CWindow::Attach( HWND hWnd )
{
	m_hWnd = hWnd;
}
void CWindow::ShowWindow( BOOL bShow )
{
	::ShowWindow( m_hWnd,bShow?SW_SHOW:SW_HIDE );
}

HWND CWindow::GetDlgItem( UINT idd )
{
	return ::GetDlgItem( m_hWnd,idd );
}

LRESULT CWindow::SendMessage( UINT msg,WPARAM wParam,LPARAM lParam )
{
	return ::SendMessage( m_hWnd,msg,wParam,lParam );
}

CListCtrlWindow::CListCtrlWindow()
{
}

void CListCtrlWindow::LVM_InsertItem( int nItem,const char* lpszItem )
{
	LVITEM item;
	item.mask = LVIF_TEXT;
	item.iItem = nItem;
	item.iSubItem = 0;
	item.pszText = (LPSTR)lpszItem;
	item.state = 0;
	item.stateMask = 0;
	item.iImage = 0;
	item.lParam = 0;
	SendMessage( LVM_INSERTITEM, 0, (LPARAM)&item);
}

void CListCtrlWindow::LVM_InsertCol( int index,const char* head,int width)
{
	LVCOLUMN column;
	column.mask = LVCF_TEXT|LVCF_FMT;
	column.pszText = (LPTSTR)head;
	column.fmt = 0;
	column.mask |= LVCF_WIDTH;
	column.cx = width;

	SendMessage( LVM_INSERTCOLUMN, index, (LPARAM)&column);
}

void CListCtrlWindow::LVM_SetItemText( int iItem,int iSubItem,const char* lpszText )
{
	LVITEM lvi;
	lvi.iSubItem = iSubItem;
	lvi.pszText = (LPTSTR) lpszText;

	SendMessage( LVM_SETITEMTEXT, iItem, (LPARAM)&lvi);
}

void CListCtrlWindow::LVM_GetItemText( int iItem,int iSubItem,char* szBuf,int nLen )
{
	LVITEM lvi;
	memset(&lvi, 0, sizeof(LVITEM));
	lvi.iSubItem = iSubItem;
	lvi.cchTextMax = nLen;
	lvi.pszText = szBuf;
	SendMessage( LVM_GETITEMTEXT, (WPARAM)iItem,(LPARAM)&lvi);
}

BOOL CDialogBox::Create( HINSTANCE hInst,UINT idd,HWND hParent,DLGPROC dlgProc )
{
	m_hWnd = ::CreateDialog( hInst,MAKEINTRESOURCE(idd),hParent,dlgProc);
	::ShowWindow( m_hWnd,SW_SHOW );
	return TRUE;
}

int  CDialogBox::DoModal( HINSTANCE hInst,UINT idd,HWND hParent,DLGPROC dlgProc )
{
	return ::DialogBox ( hInst,MAKEINTRESOURCE(idd),hParent,dlgProc);
}

CTreeViewCtrl::CTreeViewCtrl()
{
}

HTREEITEM CTreeViewCtrl::InsertItem( LPCTSTR lpszItem,HTREEITEM hParent,HTREEITEM hInsertAfter )
{
	TVINSERTSTRUCT tvis;
	tvis.hParent = hParent;
	tvis.hInsertAfter = hInsertAfter;
	tvis.item.mask = TVIF_TEXT;
	tvis.item.pszText = (LPTSTR) lpszItem;
	tvis.item.iImage = 0;
	tvis.item.iSelectedImage = 0;
	tvis.item.state = 0;
	tvis.item.stateMask = 0;
	tvis.item.lParam = 0;
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_INSERTITEM, 0, (LPARAM)&tvis);
}

HTREEITEM CTreeViewCtrl::GetNextItem(HTREEITEM hItem, UINT nCode) const
{ 
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, nCode, (LPARAM)hItem);
}

HTREEITEM CTreeViewCtrl::GetChildItem(HTREEITEM hItem) const
{ 
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); 
}

HTREEITEM CTreeViewCtrl::GetNextSiblingItem(HTREEITEM hItem) const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem); 
}

HTREEITEM CTreeViewCtrl::GetPrevSiblingItem(HTREEITEM hItem) const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_PREVIOUS, (LPARAM)hItem); 
}

HTREEITEM CTreeViewCtrl::GetParentItem(HTREEITEM hItem) const
{ 
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hItem); 
}

HTREEITEM CTreeViewCtrl::GetFirstVisibleItem() const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_FIRSTVISIBLE, 0);
}

HTREEITEM CTreeViewCtrl::GetNextVisibleItem(HTREEITEM hItem) const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, (LPARAM)hItem); 
}
HTREEITEM CTreeViewCtrl::GetPrevVisibleItem(HTREEITEM hItem) const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_PREVIOUSVISIBLE, (LPARAM)hItem); 
}

HTREEITEM CTreeViewCtrl::GetLastVisibleItem() const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_LASTVISIBLE, 0); 
}

HTREEITEM CTreeViewCtrl::GetSelectedItem() const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_CARET, 0); 
}
HTREEITEM CTreeViewCtrl::HitTest(POINT pt, UINT* pFlags) const
{
	TVHITTESTINFO hti;
	hti.pt = pt;
	HTREEITEM h = (HTREEITEM)::SendMessage(m_hWnd, TVM_HITTEST, 0,
		(LPARAM)&hti);
	if (pFlags != NULL)
		*pFlags = hti.flags;
	return h;
}
HTREEITEM CTreeViewCtrl::GetRootItem() const
{
	return (HTREEITEM)::SendMessage(m_hWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0); 
}
BOOL CTreeViewCtrl::GetCheck(HTREEITEM hItem) const
{
	TVITEM item;
	item.mask = TVIF_HANDLE | TVIF_STATE;
	item.hItem = hItem;
	item.stateMask = TVIS_STATEIMAGEMASK;
	::SendMessage(m_hWnd, TVM_GETITEM, 0, (LPARAM)&item);
	// Return zero if it's not checked, or nonzero otherwise.
	return ((BOOL)(item.state >> 12) -1);
}

BOOL CTreeViewCtrl::SetCheck(HTREEITEM hItem, BOOL fCheck)
{
	TVITEM item;
	item.mask = TVIF_HANDLE | TVIF_STATE;
	item.hItem = hItem;
	item.stateMask = TVIS_STATEIMAGEMASK;

	 // Since state images are one-based, passing a 1 to the 
	 // INDEXTOSTATEIMAGEMASK macro turns the check off, and
	// passing 2 turns it on.
	item.state = INDEXTOSTATEIMAGEMASK((fCheck ? 2 : 1));

	return (BOOL)::SendMessage(m_hWnd, TVM_SETITEM, 0, (LPARAM)&item);
}

BOOL CTreeViewCtrl::SetItemText(HTREEITEM hItem, LPCTSTR lpszItem)
{ 
	TVITEM item;
	item.hItem = hItem;
	item.mask = TVIF_TEXT;
	item.pszText = (LPTSTR) lpszItem;
	item.iImage = 0;
	item.iSelectedImage = 0;
	item.state = 0;
	item.stateMask = 0;
	item.lParam = 0;
	return (BOOL)::SendMessage(m_hWnd, TVM_SETITEM, 0, (LPARAM)&item);
}
BOOL CTreeViewCtrl::SetItemData(HTREEITEM hItem, DWORD_PTR dwData)
{
	TVITEM item;
	item.hItem = hItem;
	item.mask = TVIF_PARAM;
	item.pszText = NULL;
	item.iImage = 0;
	item.iSelectedImage = 0;
	item.state = 0;
	item.stateMask = 0;
	item.lParam = dwData;
	return (BOOL)::SendMessage(m_hWnd, TVM_SETITEM, 0, (LPARAM)&item);
}

void CTreeViewCtrl::GetItemData(HTREEITEM hItem,DWORD*pdwData ) const
{
	TVITEM item;
	item.hItem = hItem;
	item.mask = TVIF_PARAM;
	item.lParam = (LPARAM)pdwData;
	item.cchTextMax = 0;
	::SendMessage(m_hWnd, TVM_GETITEM, 0, (LPARAM)&item);	
}

BOOL CTreeViewCtrl::DeleteItem(HTREEITEM hItem)
{
	return (BOOL)::SendMessage(m_hWnd, TVM_DELETEITEM, 0, (LPARAM)hItem);
}

BOOL CTreeViewCtrl::DeleteAllItems()
{
	return (BOOL)::SendMessage(m_hWnd, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
}

BOOL CTreeViewCtrl::Expand(HTREEITEM hItem, UINT nCode)
{
	return (BOOL)::SendMessage(m_hWnd, TVM_EXPAND, nCode, (LPARAM)hItem); 
}

BOOL CTreeViewCtrl::ExpandTree( HTREEITEM hItem,UINT nCode )
{

	HTREEITEM ht1 = GetChildItem( hItem );
	
	while( ht1 )
	{
		ExpandTree( ht1,TVE_EXPAND );
		ht1 = GetNextSiblingItem( ht1 );
	}
	
	Expand( hItem,nCode );

	return TRUE;
}

void CTreeViewCtrl::GetItemText(HTREEITEM hItem,char* text,int nLen ) const
{
	TVITEM item;
	item.hItem = hItem;
	item.mask = TVIF_TEXT;

	item.pszText = text;
	item.cchTextMax = nLen;
	::SendMessage(m_hWnd, TVM_GETITEM, 0, (LPARAM)&item);	
}